<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { artDelChannelService, artGetChannelsService } from '@/api/article'
import ChannelEdit from './components/ChannelEdit.vue'
import { ElMessage } from 'element-plus'
//loading效果
const loading = ref(false)
//弹层
const dialog = ref() //获取子组件实例
//获取文章列表数据
const channelList = ref([])
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  console.log(res.data.data, '132')
  loading.value = false
}
getChannelList()

const onEditChannel = (row) => {
  dialog.value.open(row)
}
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确定删除该分类信息吗？', '温馨提示', {
    //触发事件，利用element中的弹框显示
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getChannelList()
}
const onAddChannel = () => {
  dialog.value.open({})
}
const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <!--title是组件中的，设置标题-->
    <!--插槽1-->
    <template #extra>
      <el-button type="primary" @click="onAddChannel"> 添加分类 </el-button>
    </template>
    <!--插槽2-->
    <el-table :data="channelList" style="width: 100%" v-loading="loading">
      <el-table-column label="序号" width="100" type="index"> </el-table-column
      ><!--type="index"就可以看到序号-->
      <el-table-column label="分类名称" prop="cate_name"></el-table-column
      ><!--prop就就可以往下渲染数据-->
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="100">
        <!-- row 就是 channelList 的一项，类似于item； $index 下标 -->
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!--弹层-->
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>
